package org.reenscape.updater;
import java.io.*;
import java.sql.*;
import org.reenscape.*;
import org.reenscape.lib.Library;
import org.reenscape.model.*;
import org.reenscape.lib.Configuration;

public class ItemUpdater {

	public static int showItemTimer = 60;
	public static int hideItemTimer = 60;
	public static int[] globalItemController = new int[1500001];
	public static int[] globalItemID = new int[1500001];
	public static int[] globalItemX = new int[1500001];
	public static int[] globalItemY = new int[1500001];
	public static int[] globalItemAmount = new int[1500001];
	public static boolean[] globalItemStatic = new boolean[1500001];
	public static int[] globalItemTicks = new int[1500001];


	public ItemUpdater() {			
		for (int i = 0; i <= 1500000; i++) {
			globalItemController[i] =	0;
			globalItemID[i] =			0;
			globalItemX[i] =			0;
			globalItemY[i] =			0;
			globalItemAmount[i] =		0;
			globalItemTicks[i] =		0;
			globalItemStatic[i]  =	false;
		}
		for(int i = 0; i < MaxListedItems; i++) {
			ItemList[i] = null;
		}
	}

	public static boolean getnoted(int item) {
		if (ItemList[item].itemDescription.startsWith("Swap this note at any bank for a")) {
			return true;
		}
		else {
			return false;
		}	
	}
	
	public void process() {
		for (int i = 0; i <= 1500000; i++) {
			if (globalItemID[i] != 0)
				globalItemTicks[i]++;

			if ((hideItemTimer+showItemTimer) == globalItemTicks[i]) {
				if(!globalItemStatic[i]) {
					removeItemAll(globalItemID[i], globalItemX[i], globalItemY[i]);
				}
			}

			if (showItemTimer == globalItemTicks[i]) {
				if(!globalItemStatic[i]) {
					createItemAll(globalItemID[i], globalItemX[i], globalItemY[i], globalItemAmount[i], globalItemController[i]);
				}
			}
		}
	}

	public static boolean itemExists(int itemID, int itemX, int itemY) {
		for (int i = 0; i <= 1500000; i++) {
			if(globalItemID[i] == itemID && globalItemX[i] == itemX && globalItemY[i] == itemY)
				return true;
		}
	 return false;
	}

	public static int itemAmount(int itemID, int itemX, int itemY) {
		for (int i = 0; i <= 1500000; i++) {
			if(globalItemID[i] == itemID && globalItemX[i] == itemX && globalItemY[i] == itemY)
				return globalItemAmount[i];
		}
	 return 0;
	}

	public static void addItem(int itemID, int itemX, int itemY, int itemAmount, int itemController, boolean itemStatic) {
		for (int i = 0; i <= 1500000; i++) {
			if(globalItemID[i] == 0) {
				globalItemController[i] =	itemController;
				globalItemID[i] =			itemID;
				globalItemX[i] =			itemX;
				globalItemY[i] =			itemY;
				globalItemAmount[i] =		itemAmount;
				globalItemStatic[i] =		itemStatic;
				globalItemTicks[i] =		0;
				if (globalItemController[i] != -1 && globalItemController[i] != 0 && Server.clientUpdater.players[globalItemController[i]] != null)
				spawnItem(globalItemID[i], globalItemX[i], globalItemY[i], globalItemAmount[i], globalItemController[i]);
				break;
			}
		}
	}

	public static void spawnItem(int itemID, int itemX, int itemY, int itemAmount, int playerFor) {
		Player pFor = Server.clientUpdater.players[playerFor];
		if(pFor != null){
			PacketParser person = (PacketParser) pFor;
			person.createGroundItem(itemID, itemX, itemY, itemAmount);
		}
	}

	public static void removeItem(int itemID, int itemX, int itemY, int itemAmount) {
		for (int i = 0; i <= 1500000; i++) {
			if(globalItemID[i] == itemID && globalItemX[i] == itemX && globalItemY[i] == itemY && globalItemAmount[i] == itemAmount) {
				removeItemAll(globalItemID[i], globalItemX[i], globalItemY[i]);
				globalItemController[i] =	0;
				globalItemID[i] =			0;
				globalItemX[i] =			0;
				globalItemY[i] =			0;
				globalItemAmount[i] =		0;
				globalItemTicks[i] =		0;
				globalItemStatic[i] =	false;
			}
		}
	}

	public static void createItemAll(int itemID, int itemX, int itemY, int itemAmount, int itemController) {
		for (Player p : Server.clientUpdater.players){
			if(p != null) {
				PacketParser person = (PacketParser) p;
				if((person.playerName != null || person.playerName != "null") && !(person.playerId == itemController)) {
					if (person.distanceToPoint(itemX, itemY) <= 60) {
						person.createGroundItem(itemID, itemX, itemY, itemAmount);
					}
				}
			}
		}
	}

	public static void removeItemAll(int itemID, int itemX, int itemY) {
		for (Player p : Server.clientUpdater.players){
			if(p != null) {
				PacketParser person = (PacketParser) p;
				if(person.playerName != null || person.playerName != "null") {
					if (person.distanceToPoint(itemX, itemY) <= 60) {
						person.removeGroundItem(itemX, itemY, itemID);
					}
				}
			}
		}
	}

	public static ItemList ItemList[] = new ItemList[10000];
	public static int MaxListedItems = 10000;
	public static int MaxDropItems = 1000000;
	
	public void newItemList(int ItemId, String ItemName, String ItemDescription, double ShopValue, double LowAlch, double HighAlch, int Bonuses[]) {
		// first, search for a free slot
		int slot = -1;
		for (int i = 0; i < MaxListedItems; i++) {
			if (ItemList[i] == null) {
				slot = i;
				break;
			}
		}

		if(slot == -1) return;		// no free slot found

		ItemList newItemList = new ItemList(ItemId);
		newItemList.itemName = ItemName;
		newItemList.itemDescription = ItemDescription;
		newItemList.ShopValue = ShopValue;
		newItemList.LowAlch = LowAlch;
		newItemList.HighAlch = HighAlch;
		newItemList.Bonuses = Bonuses;
		ItemList[slot] = newItemList;
	}

	public void loadItemList() {
		int count = Library.sql.count("SELECT COUNT(*) FROM `[s]itemlist`");
		System.out.println("Loading item lists....");
		try {
			ResultSet rs;
			int i = 1;
			rs = Library.sql.query("SELECT * FROM `[s]itemlist`");
			for (; rs.next(); i++){
				int aStab = rs.getInt("aStab"); int aSlash = rs.getInt("aSlash"); int aCrush = rs.getInt("aCrush");
				int aMage = rs.getInt("aMage"); int aRange = rs.getInt("aRange"); int dStab = rs.getInt("dStab");
				int dSlash = rs.getInt("dSlash"); int dCrush = rs.getInt("dCrush"); int dMage = rs.getInt("dMage");
				int dRange = rs.getInt("dRange"); int gPrayer = rs.getInt("prayer"); int gStrength = rs.getInt("str");
				int[] BonusArray = new int[12];
				BonusArray[0] = aStab; BonusArray[1] = aSlash; BonusArray[2] = aCrush; BonusArray[3] = aMage; BonusArray[4] = aRange;
				BonusArray[5] = dStab; BonusArray[6] = dSlash; BonusArray[7] = dCrush; BonusArray[8] = dMage; BonusArray[9] = dRange;
				BonusArray[10] = gStrength; BonusArray[11] = gPrayer;
				
				String name = rs.getString("name"); name.replaceAll("_", " ");
				String desc = rs.getString("desc"); desc.replaceAll("_", " ");
				
				newItemList(rs.getInt("id"), name, desc, rs.getDouble("sell"), rs.getDouble("buy"), rs.getDouble("alch"), BonusArray);
				Library.print.progress(i, count);
			}
		} catch (Exception e) {
			Library.print.error("An error occured when downloading rows from `itemlist`.", "StartUp", "load()", e);
		}
	}
}
